package com.ly.gen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.bean.vo.TreeVO;
import com.ly.gen.dto.GenCodeDTO;
import com.ly.gen.dto.GenFilePathDTO;
import com.ly.gen.entity.GenTable;
import com.ly.gen.service.IGenService;
import com.ly.gen.service.IGenTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 代码生成
 *
 * @author luoyun
 * @Date: 2025-06-12
 */
@Tag(name = "主表")
@Slf4j
@RestController
@RequestMapping("/online")
@RequiredArgsConstructor
public class GenController {

    private final IGenTableService genTableService;
    private final IGenService genService;

    /**
     * 已生成的表格
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getTablePage")
    public R<IPage<GenTable>> getTablePage(
            GenTable entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return genService.getTablePage(entity, new Page<GenTable>(pageNo, pageSize));
    }

    /**
     * 未生成的表名列表
     *
     * @return
     */
    @Operation(summary = "未生成的表名列表")
    @GetMapping("/getTableList")
    public R<List<HashMap>> getTableList(@RequestParam(name = "tableName", required = false) String tableName) {
        return genService.getTableList(tableName);
    }

    /**
     * 添加表
     *
     * @return
     */
    @Operation(summary = "添加表")
    @PostMapping("/addTables/{tableNames}")
    public R<?> addTransTables(@PathVariable("tableNames") String tableNames) {
        return genService.addTables(tableNames);
    }

    /**
     * 获取表公共信息-代码生成页面1
     *
     * @param id
     * @return
     */
    @GetMapping("/getTableInfo")
    public R<?> getTableInfo(@RequestParam(name = "id") String id) {
        return genService.getTableInfo(id);
    }

    /**
     * 代码生成
     *
     * @param dto
     * @return
     */
    @PostMapping("/getCode")
    public R<List<String>> getCode(@RequestBody GenCodeDTO dto) {
        return genService.getCode(dto);
    }

    /**
     * 代码预览-文件树
     *
     * @param dto
     * @return
     */
    @PostMapping("/getCodeViewFilePathTree")
    public R<List<TreeVO>> getCodeViewFilePathTree(@Validated @RequestBody GenFilePathDTO dto) {
        return genService.getCodeViewFilePathTree(dto);
    }


    /**
     * 代码预览-文件树
     *
     * @param path
     * @return
     */
    @GetMapping("/getCodeView")
    public void getCodeView(@RequestParam(name = "path") String path) {
        genService.getCodeView(path);
    }

    /**
     * 代码预览-文件树
     *
     * @param path
     * @return
     */
    @GetMapping("/getCodeViewText")
    public R getCodeViewText(@RequestParam(name = "path") String path) {
        return genService.getCodeViewText(path);
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody GenTable entity) {
        return genTableService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody GenTable entity) {
        return genTableService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return genService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<GenTable> dto) {
        return genTableService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return genTableService.deleteBatch(ids);
    }
}
